Community
Function mapping in a pandas dataframe
by: mnalevanko, 8 years ago
Last edited: 8 years ago
Hello,
after creating a pandas dataframe consisting of historical stock prices I am trying to add a new column that would include the volume for each day if the stock price went down from previous day or 0 otherwise.
My problem is that mapping a function doesn't return desired results. This is my code:
import pandas as pd import pandas.io.data as web import numpy as np def adjustedVolume(change, volume): if change < 0: return volume else: return 0 ticker = 'TSLA' source = 'google' start = '1/1/2015' end = '7/4/2016' df = web.DataReader(ticker, source, start, end) df['50DMA'] = pd.rolling_mean(df['Close'], 50) df['Difference'] = df['Close'].diff() df['Adjusted volume'] = map(adjustedVolume, df['Difference'], df['Volume']) print(df.tail())The script does create a new column, however the rows do not contain any numbers. What I see in each line is the following message:
<map object at 0x05567630>
Any idea what I do wrong? Thank you very much!
Mike
×
You must be logged in to post. Please login or register an account.
You need to convert to list:
df['Adjusted volume'] = list(map(adjustedVolume, df['Difference'], df['Volume']))
-Harrison 8 years ago
Reply
×
You must be logged in to post. Please login or register an account.